package com.bbc.core.model;

import com.bbc.core.annotation.ParamDesc;
import com.bbc.core.annotation.ParamIgnore;
import com.bbc.core.constants.CoreConstants;
import lombok.Data;

import java.io.Serializable;

@Data
public class R<T> implements Serializable {
   @ParamIgnore
   private static final long serialVersionUID = 5095439538716185017L;
   @ParamDesc("返回标识：成功（200）Or失败（500）")
   private int code;
   @ParamDesc("失败消息")
   private String msg;
   @ParamDesc("返回数据")
   private T data;
   @ParamDesc("是否把返回值包装成R")
   private Boolean decorate;

   public R() {
      this.decorate = Boolean.TRUE;
   }

   public static <T> R<T> ok() {
      return restResult(null, CoreConstants.SUCCESS, (String)null);
   }

   public static <T> R<T> ok(T data) {
      return restResult(data, CoreConstants.SUCCESS, (String)null);
   }

   public static <T> R<T> ok(T data, String msg) {
      return restResult(data, CoreConstants.SUCCESS, msg);
   }

   public static <T> R<T> fail() {
      return restResult(null, CoreConstants.FAIL, (String)null);
   }

   public static <T> R<T> fail(String msg) {
      return restResult(null, CoreConstants.FAIL, msg);
   }

   public static <T> R<T> fail(T data) {
      return restResult(data, CoreConstants.FAIL, (String)null);
   }

   public static <T> R<T> fail(T data, String msg) {
      return restResult(data, CoreConstants.FAIL, msg);
   }

   public static <T> R<T> fail(int code, String msg) {
      return restResult(null, code, msg);
   }

   private static <T> R<T> restResult(T data, int code, String msg) {
      R<T> apiResult = new R();
      apiResult.setCode(code);
      apiResult.setData(data);
      apiResult.setMsg(msg);
      return apiResult;
   }

}
